/**
 * 
 */
var bibao = 30;
$(document)
		.ready(
				function(){
					for (var i = 0; i < 15; i++)
						$("table")
								.append(
										"<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
					var allPlace = new Array();
					for (var i = 0; i < 15; i++)
						allPlace[i] = new Array();
					// 执黑
					$("#bFirst").click(function(){
						$("#wFirst").unbind("click");
						$("#bFirst").unbind("click");
						// alert("您选择了先手执黑");
						$("td").click(function(){
							$(this).unbind("click");
							var b = $(this).index();
							var a = $(this).parent().index();
							var local = b * 15 + a;
							$(this).css("background-image", "url('css/tdblack.jpg')");
							if (evaluate(allPlace, 1, a, b) > 5000000){
								alert("游戏结束,您赢了!");
								$("td").unbind("click");
								return;
							}
							allPlace[a][b] = 1;
							var loc2 = aiThink(allPlace, -1, bibao);
							b = loc2 % 15;
							a = (loc2 - b) / 15;
							$("tr").eq(a).find("td").eq(b).unbind("click");
							$("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdwhite.jpg')");
							allPlace[a][b] = -1;
						});
					});

					// 执白
					$("#wFirst").click(function(){
						$("#wFirst").unbind("click");
						$("#bFirst").unbind("click");
						// alert("您选择了后手执白");
						allPlace[7][7] = -1;
						$("td").eq(112).css("background-image", "url('css/tdblack.jpg')")
						$("td").click(function(){
							$(this).unbind("click");
							var b = $(this).index();
							var a = $(this).parent().index();
							var local = b * 15 + a;
							$(this).css("background-image", "url('css/tdwhite.jpg')");
							if (evaluate(allPlace, 1, a, b) > 5000000){
								alert("游戏结束,您赢了!");
								$("td").unbind("click");
								return;
							}
							allPlace[a][b] = 1;
							var loc2 = aiThink(allPlace, -1, bibao);
							b = loc2 % 15;
							a = (loc2 - b) / 15;
							$("tr").eq(a).find("td").eq(b).unbind("click");
							$("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdblack.jpg')");
							allPlace[a][b] = -1;
						});
					});

				});

// 全棋盘分析
function aiThink(arr,sign,deep){
	var score = 0,score2 = 0;
	var local = 0;
	var anti = sign * (-1);
	// 循环分析
	var my=sThink(arr,sign);
	var mysc=search(my,5000);
	if(myscore.length>0)
		return mysc[0];
	var op=sThink(arr,anti);
	var opsc=search(op,5000);
	if(opsc.length>0)
		return opsc[0];
	mysc=search(my,4000);
	if(mysc.length>0)
			return mysc[0];
	mysc=search(my,3004);
	if(mysc.length>0){
		return mysc[0];
		
	}
	
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (arr[i][j] != undefined||!check(arr, i, j) )
				continue;
			var temp = evaluate(arr, sign, i, j);
			if (temp < 20&&score<temp){
				score = temp;
				local = i * 15 + j;
				continue;
			}
			if(temp>=5000)
				return i*15+j;
			else if(temp>=4000)
				return 
			
		}
	return local;
}

function sThink(arr,sign){
	var score = 0;
	var local = -1;
	var all=new Array();
	// 循环分析
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (arr[i][j] != undefined||!check(arr, i, j) )
				continue;
			var temp = evaluate(arr, sign, i, j);
			all.push([i*15+j,temp]);
			}
	return all;
}

// 搜索
function search(all,score){
	var result=[];
	for (var j = 0; j <all.length; j++){
			if (all[j][1]==score )
				result.push(all[j][0]);
		}
	return result;
}
function bsearch(all,sign,score){
	var result=[];
	for (var i = 0; i <15; i++)
	for (var j = 0; j <15; j++){
		if (evaluate(all,sign,i,j)==score )
			result.push(i*15+j);
	}
	return result;
}

// 位置打分
function evaluate(arr,sign,a,b){
	var anti = 0 - sign;
	var score = 0;
	var troop = new Array();
	for (var i = 0; i < 4; i++)
		troop[i] = "";
	// 横
	for (var i = b - 4; i < b + 5; i++){
		if (i >= 0 && i < 15)
			if (i == b)
				troop[0] += '1';
			else if (arr[a][i] == sign)
				troop[0] += '1';
			else if (arr[a][i] == anti)
				troop[0] += '2';
			else
				troop[0] += '0';

	}

	// 竖
	for (var i = a - 4; i < a + 5; i++){
		if (i >= 0 && i < 15)
			if (i == a)
				troop[1] += '1';
			else if (arr[i][b] == sign)
				troop[1] += '1';
			else if (arr[i][b] == anti)
				troop[1] += '2';
			else
				troop[1] += '0';

	}

	// 捺\
	var i,j;
	for (i = a - 4, j = b - 4; i < a + 5 && j < b + 5; i++, j++){
		if (i >= 0 && i < 15 && j >= 0 && j < 15)
			if (i == a && j == b)
				troop[2] += '1';
			else if (arr[i][j] == sign)
				troop[2] += '1';
			else if (arr[i][j] == anti)
				troop[2] += '2';
			else
				troop[2] += '0';
	}

	// 撇/
	for (i = a - 4, j = b + 4; i < a + 5 && j > b - 5; i++, j--){
		if (i >= 0 && i < 15 && j >= 0 && j < 15)
			if (i == a && j == b)
				troop[3] += '1';
			else if (arr[i][j] == sign)
				troop[3] += '1';
			else if (arr[i][j] == anti)
				troop[3] += '2';
			else
				troop[3] += '0';
	}

	// 计算评分
	for (var i = 0; i < 4; i++){
		if (/(11111)/g.test(troop[i]))
			score += 20000000;
		else if (/(011110)|(1011101)/g.test(troop[i]))
			score += 500000;
		else if (/(11110)|(10111)|(01111)|(11101)|(11011)/g.test(troop[i]))
			score += 250000;
		else if (/(011100)|(001110)|(010110)|(011010)/g.test(troop[i]))
			score += 50000;
		else if (/(001100)|(011000)|(000110)|(010100)|(010010)|(001010)/g.test(troop[i]))
			score += 5000;
		else if (/(11100)|(10110)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)(001010)|(010010)/g
				.test(troop[i]))
			score += 500;
		else if (/(11000)|(10010)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)(001010)|(010010)/g
				.test(troop[i]))
			score += 50;
	}

	if (score > 20000000)
		return 5000;
	else if (score > 500000)
		return 4000;
	else if (score > 300000)
		return 3004;
	else if (score > 255000)
		return 2004;
	else if (score > 250500)
		return 403;
	else if (score > 250000)
		return 4;
	else if (score > 100000)
		return 3030;
	else if (score > 55000)
		return 3020;
	else if (score > 50500)
		return 3030;
	else if (score > 50000)
		return 3000;
	else if (score > 10000)
		return 2020;
	else if (score > 5500)
		return 2003;
	else if (score > 5000)
		return 2000;
	else if (score > 550)
		return 3;
	else if(score>50)
		return 2;
	else
		return 1 - Math.abs(a - 7) - Math.abs(b - 7);;
}


// 排除
function check(arr,a,b){
	var aa=a+4<15?a+4:15;
	var bb=b+4<15?b+4:15;
	for (var i = a - 4 > 0 ? a - 4 : 0; i < aa; i++)
		for (var j = b - 4 > 0 ? b - 4 : 0; j < bb; j++)
			if (arr != undefined)
				return true;
	return false;
}

